package dome3;

/**
 * Created with IntelliJ IDEA.
 * Description: 链表
 * User: 王圆豪
 * Date: 2024-07-19
 * Time: 19:05
 */
public class LinkedList<E> {
    public ListNode head;
    static class ListNode<E>{
        public E val;
        public ListNode next;

        public ListNode(E val) {
            this.val = val;
        }
    }

    //首插
    public void addFirst(E data) {
        ListNode cur = new ListNode(data);
        cur.next = head;
        head = cur;
    }

    //尾插
    public void addLast(E data) {
        if (head == null) {
            addFirst(data);
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = new ListNode(data);
    }

    //指定位置插入
    public void addIndex(int index, E data) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合理！！！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        int pos = 0;
        ListNode cur = head;
        while (pos != index - 1) {
            cur = cur.next;
            pos++;
        }
        ListNode tmp = cur.next;
        cur.next = new ListNode(data);
        cur.next.next = tmp;
    }

    //是否含有指定元素
    public boolean contains(E key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val.equals(key)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //移除第一次出现的指定元素
    public void remove(E key) {
        if (head == null) {
            return;
        }
        if (head.val.equals(key)) {
            head = head.next;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val.equals(key)) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //移除所有出现过的指定元素
    public void removeAllKey(E key) {
        if (head == null) {
            return;
        }
        while (head.val.equals(key)) {
            head = head.next;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.next.val.equals(key)) {
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
    }

    //链表长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空链表
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur = null;
            cur = tmp;
        }
        head = null;
    }

    //打印链表
    public void display() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val.toString() + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
